<!--  -->

<style lang='scss' type='stylesheet/scss' scoped>
.upload-file-warpper {
  width: 500px;
  height: 400px;
  position: fixed;
  bottom: 10px;
  right: 20px;
  background: white;
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.26);
  z-index: 2;
  box-sizing: border-box;
  overflow: hidden;
  transition: all 0.4s ease;
  &.close {
    width: 104px;
    height: 48px;
    .upload-file-header {
      transition: all 0.4s ease;
      padding-left: 10px;
    }
  }
  &.open {
    height: 400px;
    width: 500px;
  }
  .upload-file-header {
    padding: 4px 0;
    line-height: 39px;
    font-size: 16px;
    color: #333;
    padding-left: 22px;
    display: flex;
    border-bottom: solid #f5f5f5 1px;
    .name {
      flex: 1;
    }
    .float-right {
      height: 39px;
      i {
        cursor: pointer;
        line-height: 19px;
        font-size: 20px;
        color: #333;
        font-weight: bold;
        padding: 10px;
      }
    }
  }
  .upload-info {
    line-height: 30px;
    font-size: 12px;
    color: white;
    background: #33ad33;
    padding-left: 22px;
    display: block;
  }
  .upload-scroll {
    height: 322px;
    .file-item + .file-item {
      border-top: solid 1px #f5f5f5;
    }
    .file-item-warpper {
      box-sizing: border-box;
      line-height: 40px;
      padding: 10px 0 10px 22px;
      font-size: 14px;
      color: #555;
      position: relative;
      .background {
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        background: rgba(51, 173, 51, 0.18) !important;
        transition: all 0.4s ease;
        opacity: 1;
        width: 0%;
        z-index: 1;
      }
      .file-item {
        position: relative;
        z-index: 2;
        display: flex;
        img {
          flex: 0 0 32px;
          width: 32px;
          height: 32px;
          margin: 4px 0;
        }
        .name {
          flex: 1;
          padding: 0 8px;
          width: 170px;

          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .parent-name {
          flex: 1;
          padding: 0 8px;
          width: 170px;

          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .progress {
          flex: 0 0 80px;
          text-align: center;
          width: 80px;
        }
        i {
          width: 80px;
          flex: 0 0 80px;
          text-align: center;
          padding: 10px;
          line-height: 20px;
          font-size: 20px;
          box-sizing: border-box;
          cursor: pointer;
        }
      }
      &:hover {
        background: rgba(51, 173, 51, 0.18) !important;
      }
    }
  }
}
</style>

<template>
  <div class="upload-file-warpper"
       :class="triggler?'open':'close'"
       v-if="uploadVisible">
    <div class="upload-file-header">
      <span class="name"
            v-if="triggler">上传列表</span>
      <div class="float-right">
        <i :class="triggler?'el-icon-arrow-down':'el-icon-arrow-up'"
           @click="triggler=!triggler"></i>
        <i class="el-icon-close"
           @click="uploadVisible=false"></i>
      </div>
    </div>
    <span class="upload-info">
      {{loadNumber}} 文件正在上传, {{finishNumber}} 文件上传成功, {{errorNumber}} 文件上传失败
    </span>
    <el-scrollbar class="upload-scroll">
      <div class="file-item-warpper"
           v-for="item in uploadList">
        <div class="background"
             :style="`width:${item.progress};opacity:${item.progress==='100%'?'0':'1'}`"></div>
        <div class="file-item">
          <img :src="require(`../../assets/icons/${item.type.split('/')[0]}.png`)">
          <span class="name">{{item.name}}</span>
          <span class="parent-name">{{item.parentName}}</span>
          <span v-if="item.status==='load'"
                class="progress">{{item.progress!=='100%'?item.progress:'正在保存'}}</span>
          <i class="el-icon-close"
             @click="dropItem(item)"
             v-if="item.status==='finish'"></i>
          <i class="el-icon-refresh-left"
             @click="refresh(item)"
             v-if="item.status==='error'"></i>
        </div>
      </div>
    </el-scrollbar>
  </div>
</template>

<script type='text/ecmascript-6'>
import { mapGetters } from "vuex";
import axios from "axios";
export default {
  computed: {
    ...mapGetters(["uploadList"])
  },
  watch: {
    uploadList(newVal) {
      this.uploadVisible = true;
      this.$nextTick(_ => {
        if (!this.setTriggler) {
          this.setTriggler = true;
          this.triggler = true;
        }
        this.uploadFile();

        if (!newVal.length) {
          this.triggler = false;
        }
      });
    }
  },
  data() {
    return {
      setTriggler: false,
      triggler: false,
      uploadVisible: false,
      length: 0,
      excutiveList: [],
      finishList: [],
      source: [],
      loadNumber: 0,
      finishNumber: 0,
      errorNumber: 0
    };
  },
  methods: {
    dropItem(item) {
      this.uploadList.splice(this.uploadList.indexOf(item), 1);
      this.total();
    },
    refresh(item) {
      item.status = "";
      this.uploadList.splice(this.uploadList.indexOf(item), 1);
      this.uploadList.unshift(item);
      this.total();
    },
    uploadFile() {
      const CancelToken = axios.CancelToken;
      const _this = this;

      this.uploadList.forEach(async file => {
        if (file.status === "" || !file.status) {
          file.status = "load";
          _this.total();
          _this.source[file.uid] = CancelToken.source();

          let data = new FormData();
          data.append("file", file);
          data.append("parentId", file.parentId);

          axios({
            method: "post",
            url: _this.$host + "/api/file/upload-file",
            withCredentials: true,
            cancelToken: _this.source[file.uid].token,
            headers: {
              "Content-Type": "multipart/form-data",
              Authorization: localStorage.getItem("I-Token")
            },
            data,
            onUploadProgress: progress => {
              console.log(progress);

              file.progress =
                (((progress.loaded / progress.total) * 100) | 0) + "%";

              _this.$forceUpdate();
            }
          })
            .then(response => {
              file.status = "finish";
              // 统计上传数据
              _this.total();

              this.$store.commit("file/ADD_FILE", response.data.data);
            })
            .catch(error => {
              file.status = "error";
              // 统计上传数据
              _this.total();
            });
        }
      });
    },

    total() {
      this.finishNumber = this.uploadList.filter(
        e => e.status === "finish"
      ).length;
      this.loadNumber = this.uploadList.filter(e => e.status === "load").length;
      this.errorNumber = this.uploadList.filter(
        e => e.status === "error"
      ).length;

      // 更新数据
      this.$forceUpdate();
    }
  }
};
</script>
